﻿using LiveCharts.Defaults;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace CustomWPFControl.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly LinearGradientBrush txBrush = new LinearGradientBrush(
            Color.FromRgb(0x27, 0xca, 0xff), Color.FromRgb(0x00, 0xe1, 0xa3), 0);
        private readonly LinearGradientBrush rxBrush = new LinearGradientBrush(
            Color.FromRgb(0xff, 0x91, 0x00), Color.FromRgb(0xff, 0x5a, 0x00), 0);

        public List<ObservablePoint> txPoints = new List<ObservablePoint>
        {
            new ObservablePoint(1, 0),
            new ObservablePoint(2, 1),
            new ObservablePoint(3, 1),
            new ObservablePoint(4, 1),
            new ObservablePoint(5, 1),
            new ObservablePoint(6, 8),
            new ObservablePoint(7, 1),
            new ObservablePoint(8, 1),
            new ObservablePoint(9, 9),
            new ObservablePoint(10, 1),
            new ObservablePoint(11, 1),
            new ObservablePoint(12, 6),
            new ObservablePoint(13, 5),
            new ObservablePoint(14, 5),
            new ObservablePoint(15, 4),
            new ObservablePoint(16, 3),
        };

        public List<ObservablePoint> rxPoints = new List<ObservablePoint>
        {
            new ObservablePoint(1, 4),
            new ObservablePoint(2, 4),
            new ObservablePoint(3, 4),
            new ObservablePoint(4, 1),
            new ObservablePoint(5, 1),
            new ObservablePoint(6, 3),
            new ObservablePoint(7, 2),
            new ObservablePoint(8, 1),
            new ObservablePoint(9, 4),
            new ObservablePoint(10, 4),
            new ObservablePoint(11, 1),
            new ObservablePoint(12, 1),
            new ObservablePoint(13, 8),
            new ObservablePoint(14, 6),
            new ObservablePoint(15, 1),
            new ObservablePoint(16, 1),
        };

        private int testPointX = 0;
        private Random random = new Random();

        public MainWindow()
        {
            InitializeComponent();

            AddDefaultSeries(null, null);
            AddDefaultChannel(null, null);
        }

        private void DragWindow(object sender, MouseButtonEventArgs args)
        {
            DragMove();
        }

        private void Setting(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("点击了设置按钮");
        }

        private void Minimal(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void Maximal(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void AddDefaultSeries(object sender, RoutedEventArgs e)
        {
            lineChart.AddSeries("Rx", rxBrush, rxPoints);
            lineChart.AddSeries("Tx", txBrush, txPoints);
        }

        private void RemoveDefaultSeries(object sender, RoutedEventArgs e)
        {
            lineChart.RemoveSeries("Rx");
            lineChart.RemoveSeries("Tx");
        }

        private void AddSeries(object sender, RoutedEventArgs e)
        {
            lineChart.AddSeries("Test", new SolidColorBrush(Colors.Purple));
        }

        private void RemoveSeries(object sender, RoutedEventArgs e)
        {
            lineChart.RemoveSeries("Test");
            testPointX = 0;
        }

        private void AddItem(object sender, RoutedEventArgs e)
        {
            lineChart.AddPoint("Test", new ObservablePoint(testPointX++, random.NextDouble() * 20));
        }

        private void RemoveItem(object sender, RoutedEventArgs e)
        {
            testPointX = 0;
            lineChart.ClearPoints("Test");
        }

        private void OnSmooth(object sender, RoutedEventArgs e)
        {
            var smooth = sender as ToggleButton;
            smooth.Content = (bool)smooth.IsChecked ? "尖锐" : "平滑";
            lineChart.SetSmoothness((bool)smooth.IsChecked);
        }

        private void ResetZoom(object sender, RoutedEventArgs e)
        {
            lineChart.ResetZoom();
        }

        private void AddDefaultChannel(object sender, RoutedEventArgs e)
        {
            channelChart.AddSeries("TP-Link 229", 1, 4, -65, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 230", 1, 4, -68, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 231", 4, 4, -83, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 232", 6, 4, -88, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 233", 9, 4, -85, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 234", 11, 4, -75, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 235", 11, 4, -78, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 236", 11, 4, -81, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 237", 11, 4, -84, ChannelChart.ChannelType.T2_4);
            channelChart.AddSeries("TP-Link 238", 11, 4, -89, ChannelChart.ChannelType.T2_4);
        }

        private void RemoveDefaultChannel(object sender, RoutedEventArgs e)
        {
            channelChart.RemoveSeries("TP-Link 229", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 230", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 231", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 232", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 233", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 234", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 235", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 236", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 237", ChannelChart.ChannelType.T2_4);
            channelChart.RemoveSeries("TP-Link 238", ChannelChart.ChannelType.T2_4);
        }

        private void AddChannel(object sender, RoutedEventArgs e)
        {
            channelChart.AddSeries("TP-Link 110", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 111", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 112", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 113", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 114", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 115", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 116", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 117", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 118", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.AddSeries("TP-Link 119", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
        }

        private void EditChannel(object sender, RoutedEventArgs e)
        {
            channelChart.UpdateValue("TP-Link 110", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 111", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 112", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 113", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 114", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 115", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 116", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 117", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 118", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
            channelChart.UpdateValue("TP-Link 119", random.Next(36, 165), 4, random.Next(-90, -30), ChannelChart.ChannelType.T5);
        }

        private void RemoveChannel(object sender, RoutedEventArgs e)
        {
            channelChart.RemoveSeries("TP-Link 110", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 111", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 112", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 113", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 114", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 115", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 116", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 117", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 118", ChannelChart.ChannelType.T5);
            channelChart.RemoveSeries("TP-Link 119", ChannelChart.ChannelType.T5);
        }

        private void OnChannelSmooth(object sender, RoutedEventArgs e)
        {
            var smooth = sender as ToggleButton;
            smooth.Content = (bool)smooth.IsChecked ? "尖锐" : "平滑";
            channelChart.SetSmoothness((bool)smooth.IsChecked);
        }
    }
}
